package ar.com.angrymachine.hunter.activities;

import java.awt.Graphics;
import java.awt.Rectangle;
import ar.com.angrymachine.hunter.Activity;
import ar.com.angrymachine.hunter.ActivitySet;
import ar.com.angrymachine.hunter.Game;
import ar.com.angrymachine.hunter.GraphicStyles;
import ar.com.angrymachine.hunter.Locale;
import ar.com.angrymachine.hunter.gameplay.Treasure;

public class TreasureActivity extends Activity
{
    private Treasure treasure;
    private int goldToGiveEachUpdate = 10;
    
    public TreasureActivity(Treasure treasure)
    {
        this.treasure = treasure;
        setGold(treasure.gold);
    }
    
    private void addGold(int gold)
	{
    	setGold( treasure.gold + gold);
	}
    
    private void setGold(int gold)
    {
    	treasure.gold =  gold;
    }
    
    public void onAddToActive(ActivitySet set)
    {
    	if(treasure.object == null)
    	{
        	// search for another active treasure activity and
        	// just add the gold
            for (int i = 0; i < set.activities.size(); i++)
            {
                Activity activity = (Activity) set.activities.elementAt(i);
                if(activity != this)
                {
	                if(activity.getClass().equals(getClass()) && !activity.isCompleted())
	                {
	                	TreasureActivity ta = (TreasureActivity) activity;
	                	ta.addGold(treasure.gold);
	                	end();
	                }
                }
            }
    	}
    }
    
	public String getDescription()
    {
    	return Locale.getTreasureDescriptionWithGold(treasure);
    }

    public String getActionText()
    {
        if(isCompleted())
        {
            return Locale.TAKEN;
        }
        else
        {
        	if(isRunning())
        	{
        		return Locale.TAKING;
        	}
        	else
        	{
        		return Locale.TAKE;
        	}
        }
    }    

    public void paint(Graphics g, Rectangle activityArea)
    {
        GraphicStyles.paintDefaultActivity(g, this, activityArea, treasure.icon);
    }

    public void update(int delta, boolean action, boolean timeIsRunning)
    {
        if(action)
        {
            int finalGold = Math.min(goldToGiveEachUpdate, treasure.gold);
            Game.current.player.gold += finalGold;
            treasure.gold -= finalGold;
            if(isCompleted())
            {
            	end(true);
            }
        }
    }

    public boolean isCompleted()
    {
        return treasure.gold <= 0;
    }
}
